package com.qf.model.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.dto.PageDto;
import com.qf.model.entity.ModelTemplate;
import com.qf.model.mapper.ModelTemplateMapper;
import com.qf.model.service.ModelTemplateService;
import com.qf.vo.PageResult;
import com.qf.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 物模型模板表 服务实现类
 * </p>
 *
 * @author xjj
 * @since 2023-10-31
 */
@Service
public class ModelTemplateServiceImpl extends ServiceImpl<ModelTemplateMapper, ModelTemplate> implements ModelTemplateService {

    @Autowired
    private ModelTemplateMapper modelTemplateMapper;

    @Override
    public PageResult<ModelTemplate> pageInfo(PageDto<ModelTemplate> pageDto) {
        String propertyName = null;
        Long deviceTypeId = null;
        Integer modelType = null;
        //条件判断
        if (!StringUtils.isEmpty(pageDto.getWhere().getPropertyName())){
            propertyName = pageDto.getWhere().getPropertyName();
        }
        if (!StringUtils.isEmpty(pageDto.getWhere().getDeviceTypeId())){
            deviceTypeId = pageDto.getWhere().getDeviceTypeId();
        }
        if (!StringUtils.isEmpty(pageDto.getWhere().getModelType())){
            modelType = pageDto.getWhere().getModelType();
        }
        //获取所有的ModelTemplate列表
        List<ModelTemplate> modelTemplateList = modelTemplateMapper.findAll(pageDto, propertyName, deviceTypeId, modelType);
        //查询总条数
        Long count = modelTemplateMapper.count(pageDto, propertyName, deviceTypeId, modelType);
        //计算总页数
        Long pages = (count + pageDto.getSize() - 1) / pageDto.getSize();
        //分装数据
        return new PageResult<>(count,pages,modelTemplateList);
    }

    @Override
    public Result add(ModelTemplate modelTemplate) {
        if (!StringUtils.isEmpty(modelTemplate)){
            modelTemplate.setCreateTime(new Date());
            modelTemplate.setUpdateTime(new Date());
            int insert = modelTemplateMapper.insert(modelTemplate);
            if(insert == 1){
                return new Result(0, "添加成功！");
            }else{
                return new Result(1, "添加失败！非法数据");
            }
        } else{
            return new Result(1, "添加失败！非法数据");
        }
    }
}
